package com.crystal.jcms.shpz;

import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;

import com.crystal.constant.Constant;
import com.crystal.jcms.utils.DialogUtils;
import com.crystal.jcms.utils.NetUtils;
import com.crystal.jcms.utils.PhoneUtils;
import com.crystal.jcms.utils.RequestUtils;
import com.crystal.jcms.utils.SpUtils;
import com.crystal.jcms.utils.UpdateManager;
import com.crystal.jcms.utils.ShowMsgUtils;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class ActivityLogin extends Activity {
	private Context mContext;
	private EditText tvUserId, tvPassword;
	private Button btnlogin, btnSet;
	private CheckBox cbRemember;
	public Dialog mLoading;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		// checkUpdate();
		initWidget();
		initLogin();
	}

	private void checkUpdate() {
		if (NetUtils.isConnected(mContext)) {
			UpdateManager updateManager = new UpdateManager(mContext);
			updateManager.CheckUpdateInfo();
		}
	}

	private void initWidget() {
		mContext = this;
		btnlogin = (Button) findViewById(R.id.btnlogin);
		btnSet = (Button) findViewById(R.id.btnset);
		tvUserId = (EditText) findViewById(R.id.txtUserId);
		tvPassword = (EditText) findViewById(R.id.txtPassword);
		cbRemember = (CheckBox) findViewById(R.id.cbRember);
		TextView verName = (TextView) findViewById(R.id.txtVersionCode);
		verName.setText(PhoneUtils.getVersionName(mContext));
		btnlogin.setOnClickListener(new loginOnClicklistener());
		btnSet.setOnClickListener(new btnSetOnClick());
	}

	private void initLogin() {
		if (SpUtils.contains(mContext, "IsRemember")) {
			cbRemember.setChecked(false);
			tvUserId.setText((String) SpUtils.get(mContext, "UserId", ""));
			boolean isRemember = (Boolean) SpUtils.get(mContext, "IsRemember", false);
			if (isRemember) {
				tvPassword.setText((String) SpUtils.get(mContext, "Password", ""));
				cbRemember.setChecked(true);
			}
		}
	}

	public class loginOnClicklistener implements OnClickListener {

		@Override
		public void onClick(View v) {
			String userId = String.valueOf(tvUserId.getText());
			String password = String.valueOf(tvPassword.getText());
			final Message msg = new Message();
			msg.what = Constant.SHOWTOAST;
			if (userId.equals("")) {
				msg.obj = "用户名不能为空!";
				handler.sendMessage(msg);
				return;
			}
			if (password.equals("")) {
				msg.obj = " 密码不能为空!";
				handler.sendMessage(msg);
				return;
			}
			mLoading = DialogUtils.createLoadingDialog(mContext, "正在登录...");
			mLoading.show();
			Map<String, String> param = new HashMap<String, String>();
			param.put("username", userId);
			param.put("password", password);
			RequestUtils.doPostAsyn(mContext, "login.xml", "CheckLoginInfo", param, new RequestUtils.CallBack() {

				@Override
				public void onRequestComplete(String result) {
					Message message = new Message();
					message.what = Constant.SHOWTOAST;
					try {
						if (result.contains("500_")) {
							message.obj = result.substring(4);
							handler.sendMessage(message);
							return;
						}
						JSONObject jsonResult = new JSONObject(result);
						if (jsonResult.getString("ReturnType").equals("1")) {
							JSONObject json = jsonResult.getJSONObject("ReturnData");
							SpUtils.put(mContext, "Id", json.getString("Uid"));
							SpUtils.put(mContext, "UserName", json.getString("UserName"));
							SpUtils.put(mContext, "UserId", json.getString("UserId"));
							SpUtils.put(mContext, "Password", json.getString("Password"));
							SpUtils.put(mContext, "IsRemember", cbRemember.isChecked());
							msg.what = Constant.SHOWPAGE;
							handler.sendMessage(msg);
						} else {
							msg.what = Constant.SHOWMESSAGE;
							msg.obj = jsonResult.getString("ReturnMessage");
							handler.sendMessage(msg);
						}
					} catch (Exception e) {
						msg.what = Constant.SHOWMESSAGE;
						msg.obj = e.getMessage();
						handler.sendMessage(msg);
					}
				}
			});
		}
	}

	public class btnSetOnClick implements OnClickListener {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(ActivityLogin.this, ActivitySetting.class);
			startActivity(intent);
			ActivityLogin.this.finish();
		}

	}

	@SuppressLint("HandlerLeak")
	public Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (mLoading != null) {
				if (mLoading.isShowing()) {
					mLoading.dismiss();
				}
			}
			switch (msg.what) {
			case Constant.SHOWPAGE:
				Intent intent = new Intent(mContext, ActivityFrame.class);
				startActivity(intent);
				ActivityLogin.this.finish();
				break;
			case Constant.SHOWTOAST:
				ShowMsgUtils.ShowToast(mContext, (String) msg.obj);
				break;
			case Constant.SHOWMESSAGE:
				ShowMsgUtils.ShowMessage(mContext, (String) msg.obj);
				break;
			}
		}
	};
}
